package com.acme.bankapp.domain.bank.client;

import java.util.Arrays;

import com.acme.bankapp.domain.bank.account.Account;

@SuppressWarnings("serial")
public class Client implements java.io.Serializable {
	public final static int MAX_ACCOUNT = 3;
	
	private int countAccounts = 0;
	
	public int getCountAccounts() {
		return countAccounts;
	}
	
	private Account[] accounts;
	
	public void addAccount(Account account) throws ArrayIndexOutOfBoundsException {
		if (countAccounts == MAX_ACCOUNT)
			throw new ArrayIndexOutOfBoundsException("Client already contains the maximum number of accounts");
		accounts[countAccounts] = account;
		countAccounts++;
	}
	
	private String name;
	
	private Gender gender;
	
	/**
	 * 
	 * @param name name of client
	 * @param isMan is man - true, false - woman
	 */
	public Client(String name, Gender gender) {
		this.name = name;
		this.gender = gender;
		this.accounts = new Account[MAX_ACCOUNT];
	}
	
	public Account[] getAccounts() {
		return Arrays.copyOf(accounts, countAccounts);
	}
	
	public String getClientSalution() {
		return gender.getAppeal() + " " + name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(accounts);
		result = prime * result + countAccounts;
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (gender != other.gender)
			return false;
		if (countAccounts != other.countAccounts)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (!Arrays.equals(accounts, other.accounts))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuffer text = new StringBuffer(this.getClientSalution());
		text.append(". Accounts:");
		for (int i = 0; i < this.countAccounts; i++) {
			text.append(" " + accounts[i].toString() + ";");
		}
		return text.toString();
	}
}